
import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 *
 * @author arcsight7
 */
public class StartingClass extends Applet implements Runnable, KeyListener {

    Player player;
    Image image;
    Graphics second;
    Image mainBacground;
    MovingBackground bgLayer1, bgLayer2;
    Player2 player2;

    @Override
    public void init() {
        // Init Content
        Content.width = 800;
        Content.height = 480;
        // Init Frame
        setSize(Content.width, Content.height);
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Test Game");

        // Init Variables
        player = new Player();
        player2 = new Player2();
        bgLayer1 = new MovingBackground();
        bgLayer2 = new MovingBackground();

        // Load Content
        loadContent();
    }

    public void loadContent() {
        // Player
        Animation playerAnimation = new Animation();
        Image playerTexture = Content.Load("shipAnimation.png");
        playerAnimation.init(playerTexture, Vector2.Zero, 115, 69, 8, 80, 1f, true);
        Vector2 playerPosition = new Vector2(0, getHeight() / 2);
        player2.init(playerAnimation, Vector2.Zero);
        player.init(Content.Load("player.png"), playerPosition);
//        player.init(playerAnimation, playerPosition);
//
//        // Load Background
        bgLayer1.init(Content.Load("bgLayer1.png"), Content.width, -1);
        bgLayer2.init(Content.Load("bgLayer1.png"), Content.width, -2);

        mainBacground = Content.Load("mainbackground.png");
    }

    @Override
    public void start() {
        Thread t = new Thread(this);
        t.start();
    }

    @Override
    public void run() {
        while (true) {
            // update 
            player.update();
            player2.update(System.currentTimeMillis());
            bgLayer1.update();
            bgLayer2.update();
            
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException ex) {
            }
        }
    }

    @Override
    public void update(Graphics g) {
        if (image == null) {
            image = createImage(this.getWidth(), this.getHeight());
            second = image.getGraphics();
        }

        second.setColor(getBackground());
        second.fillRect(0, 0, getWidth(), getHeight());
//        second.setColor(getForeground());
        paint(second);

        g.drawImage(image, 0, 0, this);

    }

    @Override
    public void paint(Graphics g) {

        g.drawImage(mainBacground, 0, 0, Color.WHITE, null);
        
        // draw moving background
        bgLayer1.draw(g);
        bgLayer2.draw(g);
        //draw player
        player.draw(g);
        player2.draw(g);
    }

    @Override
    public void stop() {
        super.stop();
    }

    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {

        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                player.moveUp();
                break;

            case KeyEvent.VK_DOWN:
                player.moveDown();
                break;

            case KeyEvent.VK_LEFT:
                player.moveLeft();
                break;

            case KeyEvent.VK_RIGHT:
                player.moveRight();
                break;

            case KeyEvent.VK_SPACE:
                break;

        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:

                break;

            case KeyEvent.VK_DOWN:

                break;

            case KeyEvent.VK_LEFT:

                break;

            case KeyEvent.VK_RIGHT:

                break;

            case KeyEvent.VK_SPACE:

                break;

        }
    }
}
